﻿/*
   @Description:客源详细页js
   @Author:
   @Date:2014-02-13  
 */

var CustomerDetails = {};
CustomerDetails.closeContant = function (ele) {
    $(ele).parent().remove();
};
//客户联系人电话弹层事件
CustomerDetails.seeContacter = function (ele) {
    var reqData = {
        'CustomerContactKeyId': $(ele).attr('customercontactkeyid'),
        'InquiryKeyId': $(ele).attr('inquirykeyid')
    };
    $.AjaxAgency('/Customer/inquiry-contacts-infos-ajax', reqData, function (data) {
        if ((typeof data) == 'object') {
            $(ele).next('span').html('<div style=" margin-left:-2px;background-color: white; border: 1px solid #eaeaea; z-index: 9909; padding:3px 10px;max-width: 260px; position: absolute;width:auto !important;width:260px;"><a class="mobileClose r" title="关闭" onclick="CustomerDetails.closeContant(this);">×</a><label>手机:' + data[0] + '</label>&nbsp;<label>座机:' + data[1] + '</label></div>');
        } else {
            $.OKDialogAgency('<div class="tc">请求错误，请重新请求,' + data + '！</div>');
        }
    });
};

$(function () {
    //初始化客户需求样式
    $('#require ul li:not(.wfull):nth-child(4n)').addClass('w150');

    //删除客户
    $('#btnDeleteCustomer').click(function () {
        //if (!confirm('您确定要删除吗？')) {
        //    return false;
        //}
        $.OKCancelDialogAgency("您确定要删除吗？", "删除客户", true, function () {
            var selectKeyId = $('#customerKeyid').val();
            var reqData = {
                'keyIds': selectKeyId
            };
            $.AjaxAgency('/customer/inquiry-batch-remove-ajax', reqData, function (data) {
                if (data == 'ok') {
                    $.artDialog({
                        id: "artDialogBatchRemoveInquiry",
                        time: 1,
                        content: '<div class="tc">删除成功！</div>'
                    });

                    window.opener = null;
                    window.open('', '_self');
                    window.close();
                    history.back();
                } else {
                    $.artDialog({
                        id: "artDialogBatchRemoveInquiry",
                        lock: true,
                        content: data
                    });
                }
            });
        });
    });

    //发送短信模块
    $('#btnCustomerDetailsSms').click(function () {
        $.ajax({
            type: 'get',
            url: '/customer/SendMessage',
            success: function (data) {
                $.dialog({
                    id: "dialogCustomerSms",
                    lock: true,
                    content: data
                });
            }
        });
    });


    //更多客户信息
    $(".more-custom-msg").toggle(function () {
        $("#moreCustomMsg").show(500);
        $(this).find("em").removeClass().addClass("hidsel mr0");
    }, function () {
        $("#moreCustomMsg").hide(500);
        $(this).find("em").removeClass().addClass("showsel mr0");
    });

    //匹配房源信息
    $('.match-house').bind('click', function () {
        $.dialog({
            id: "matchHouse",
            lock: true,
            title: "匹配房源",
            content: $("#matchHouse").html()
        });
    });


    //转客操作
    $('#customerTransferCustomerDetail').click(function () {
        $.dialog({
            id: "CustomertransferCustom",
            title: "转客",
            lock: true,
            content: $("#transferCustom").html()
        });
        TransferCustomer.inte();
        agencyVerify.notNullInput(null, ".aui_content input[isnull=1]", false, "change");
        agencyVerify.notdefaultSelect(null, ".aui_content #selPublicDepartment", -1, "请选择公盘帐号", false);
        TransferCustomer.getCustomerIds = function () {
            var keyIds = new Array();
            keyIds[0] = $("#customerKeyid").val();
            return keyIds;
        };
        TransferCustomer.successed = function () {
            $.OKDialogAgency('<div class="tc">转客成功！</div>', '提示', function () {
                $.artDialog({ id: 'CustomertransferCustom' }).close();
            });
        };
    });

    //状态更改
    $("#btnCustomerStatusModify").click(function () {
        if (!$(this).attr("publiccustomer") && !customerRight.CheckRightRol($("#userInfo"), "Customer.Inquiry.Modify", true)) {
            return false;
        }

        if ($("#inquiryStatusKeyIdAll").css("display") == "none") {
            $("#inquiryStatusKeyIdAll").show();
        } else {
            $("#inquiryStatusKeyIdAll").hide();
        }

        CustomerCommon.showAndHide('#btnCustomerStatusModify', 'inquiryStatusKeyIdAll');

        $('#btnChangeCustomerStatu').unbind('click');
        $('#btnChangeCustomerStatu').click(function () {
            $(this).attr('disabled', 'disabled');
            var targetStatusKeyId = $("#inquiryStatusKeyIdAll input[type=radio]:checked").val();
            if (!!targetStatusKeyId) {
                $.ajax({
                    url: '/Customer/inquiry-status-change-ajax',
                    type: 'post',
                    data: { 'targetStatusKeyId': targetStatusKeyId, 'keyIds': $('#customerKeyid').val() },
                    success: function (data) {
                        if (data == 'ok') {
                            $('#btnChangeCustomerStatu').removeAttr('disabled');
                            $.OKDialogAgency('<div class="tc">设置成功！</div>', '提示', function () {
                                $("#inquiryStatusKeyIdAll").hide();
                            });
                        } else {
                            $('#btnChangeCustomerStatu').removeAttr('disabled');
                            $.OKDialogAgency('<div class="tc">' + data + '</div>');
                        }
                    }
                });
            }
            else {
                $.OKDialogAgency('<div class="tc">请选择要修改的状态！</div>');
                $('#btnChangeCustomerStatu').removeAttr('disabled');
            }
        });
    });

    detailTab();

    var option = {
        titleID: "titleid",
        contentID: "contentdiv",
        currentClassName: "current",
        titleFun: getTitles,
        urlFun: getTabUrl,
        dataFun: getData,
        contentFun: null,
        isIframe: true,
        autoMatch:1
    };
    tabHelper(option);
    //选择房源弹框 选择操作
    $(".sel-house-sel").bind("click", function () {
        $.dialog({
            id: "selHouseSel",
            lock: true,
            content: $("#selHouseSel").html()
        });
    });

});

/*房源详情tab*/
function detailTab() {
    var editTabMenu = $(".tab-nav li");
    var editTabCont = $(".tab-bd .tab-panel");
    editTabMenu.each(function () {
        $(this).click(function () {
            var index = editTabMenu.index(this);
            $(this).addClass("current").siblings().removeClass("current");
            editTabCont.eq(index).css("display", "block").siblings().css("display", "none");
        });
    });
};


function getTitles() {
    var titles = ["客户跟进", "带看记录", "用户行为"];
    if ($("#hidPermissions").val().indexOf("Customer.Inquiry.SearchLog") > -1) {
        titles.push("操作日志");
    }
    return titles;
};

function getTabUrl(tabname) {
    if (tabname == "客户跟进") {
        return "/Customer/inquiry-detail-follow?keyid=" + $("#customerKeyid").val() + "&inquiryCategory=" + $("#inquiryCategory").val() + "&isPrivateCustomer=" + $("#isPrivateCustomer").val();
    }
    if (tabname == "带看记录") {
        return "/Customer/inquiry-take-see-record?InquiryKeyId=" + $("#customerKeyid").val();
    }
    if (tabname == "用户行为") {
        return "/Customer/inquiry-user-action?keyid=" + $("#customerKeyid").val() + "";
    }
    if (tabname == "操作日志") {
        return "/Customer/inquiry-logs?InquiryKeyId=" + $("#customerKeyid").val();
    }
    if (tabname == "历史记录") {
        return "/Customer/inquiry-history-record";
    }
    return "/Common/Error404";
};

function getData(tabname) {
    return [{ name: "key", value: tabname }];
};